package com.chookss.view;

import android.content.Context;
import android.content.Intent;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.chookss.R;
import com.chookss.Urls;
import com.chookss.home.other.StaticWebActivity;
import com.chookss.view.listener.SelectTwoDialogListener;

import butterknife.BindView;


public class ProtocolDialog extends BaseDialog {

    @BindView(R.id.tvInit)
    TextView tvInit;
    @BindView(R.id.tvCancel)
    TextView tvCancel;
    @BindView(R.id.tvSure)
    TextView tvSure;
    private Context context;
    private SelectTwoDialogListener listener = null;

    public ProtocolDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_protocol;
    }

    @Override
    protected void init(View view) {
        initCenter(0.4f);
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onSure();
                dismiss();
            }
        });
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onCancel();
                dismiss();
            }
        });
    }

    public ProtocolDialog(@NonNull Context context,  SelectTwoDialogListener listener) {
        super(context);
        this.context = context;
        this.listener = listener;
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        tvInit.setText(context.getResources().getString(R.string.protocol_init2));
        SpannableString spStr = new SpannableString("《用户协议》和《隐私政策》");
        String s1 = "《用户协议》";
        String s2 = "《隐私政策》";
        ClickableSpan clickSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Intent intent = new Intent(context,
                        StaticWebActivity.class);
                intent.putExtra("title", "用户协议");
                if(context.getPackageName().equals("com.zhaofengnum")){
                    intent.putExtra("url", "http://dev.zhongyuanliantuo.com:38012/zhaofeng_license.html");
                }else{
                    intent.putExtra("url", Urls.license);
                }
                context.startActivity(intent);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(false);
                ds.setColor(context.getResources().getColor(R.color.main_color));
            }
        };
        ClickableSpan clickSpan2 = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Intent intent = new Intent(context,
                        StaticWebActivity.class);
                intent.putExtra("title", "隐私政策");
                if(context.getPackageName().equals("com.zhaofengnum")){
                    intent.putExtra("url", "file:///android_asset/zhaofeng_privacy.html");
                }else{
                    intent.putExtra("url", "file:///android_asset/chookss_privacy.html");
                }
                context.startActivity(intent);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(false);
                ds.setColor(context.getResources().getColor(R.color.main_color));
            }
        };
        spStr.setSpan(clickSpan, 0, s1.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spStr.setSpan(clickSpan2, spStr.length() - s2.length(), spStr.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        tvInit.append(spStr);
        SpannableString spStr2 = new SpannableString("的全部内容，同意并接受全部条款后开始使用我们的产品和服务。");
        tvInit.append(spStr2);
        tvInit.setMovementMethod(LinkMovementMethod.getInstance());
        tvInit.setLongClickable(false);
    }
}